package com.example.demo.controller;

import com.example.demo.entity.Contract;
import com.example.demo.entity.Designer;
import com.example.demo.entity.Measure;
import com.example.demo.entity.Plan;
import com.example.demo.service.MeasureService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/measure")
public class MeasureController {
    @Resource
    private MeasureService measureService;

    @RequestMapping("")
    public String measureListShow(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "measureList";
    }

    @RequestMapping(value = "/measureList",method = RequestMethod.POST)
    @ResponseBody
    private Map measureList(HttpSession session){
        Map map = new HashMap();
        Designer designer =(Designer) (session.getAttribute("designer"));
        String desId = designer.getDesId();
        List<Measure> measureList = measureService.getAllMeasureByDesId(desId);
        if(measureList==null || measureList.size()==0){
            map.put("msg",0);
        } else {
            map.put("msg",1);
            map.put("measureList",measureList);
        }
        return map;
    }

    @RequestMapping(value = "measureAdd",method = RequestMethod.GET)
    private String measureAdd(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "measureAdd";
    }

    @RequestMapping(value = "/measureAdd",method = RequestMethod.POST)
    @ResponseBody
    private Integer measureAdd(Measure measure){
        Integer rel = measureService.measureAdd(measure);
        return rel;
    }

    @RequestMapping(value = "/measureDetail/{id}",method = RequestMethod.GET)
    private String measureDetail(@PathVariable(name = "id")Integer id, HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        } else {
            Measure measure = measureService.getMeasureById(id);
            session.setAttribute("measure",measure);
            return "measureDetail";
        }
    }

    @RequestMapping(value = "/measureDetail",method = RequestMethod.GET)
    @ResponseBody
    private Measure measureDetail(HttpSession session){
        Measure measure =(Measure) (session.getAttribute("measure"));
        return measure;
    }

    @RequestMapping(value = "/measureModify",method = RequestMethod.POST)
    @ResponseBody
    private Integer measureModify(Measure measure){
        Integer rel = measureService.modifyMeasureById(measure);
        return rel;
    }

    @RequestMapping("/mysearch")
    @ResponseBody
    public List<Measure> mysearch(String keyword,HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        List<Measure> measureList = measureService.getMeasure(keyword,designer.getDesId());
        return measureList;
    }
}
